/**
 * This file is part of iSecurePasswords.
 *
 *   iSecurePasswords is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   iSecurePasswords is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with iSecurePasswords.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Matthew MacGregor
 * Created: 2012
 * iSecurePasswords, copyright 2013 Sudolink, LLC
 * 
 */ 
package com.sudolink.isp.ui;

import java.util.Random;

/**
 *
 * @author dev
 */
public class PasswordGeneratorManager {
    
    private boolean isUsingSpecialCharacters = true;
    private boolean isUsingUpperCaseCharacters = true;
    private boolean isUsingLowerCaseCharacters = true;
    private boolean isUsingNumericCharacters = true;
    private Random randomGenerator;
    
    public char[] generatePassword(int length) {
        char[] pw = new char[length];
        for (int i = 0; i < length; i++ ) {
            pw[i] = fetchNextCharacter();
        }
        return pw;
    }
    
    private char fetchNextCharacter() {
        if ( randomGenerator == null ) {
            randomGenerator = new Random();
        }
        int randomFactor = randomGenerator.nextInt(10);
        int random = randomGenerator.nextInt(93) + 33;
        char returnCharacter = 0;
        char[] specialCharacters = {'&', '*', '%', '$', '#', '@', '!', '^', '+', '_', '=', '?', '<'};
        int numberSpecialCharacters = 0;
        
        if (isUsingSpecialCharacters) {
            if ( randomFactor < 3 && numberSpecialCharacters < 3) {
 
               int r = randomGenerator.nextInt(12);
               if ( r < specialCharacters.length ) {
                    returnCharacter = specialCharacters[r];
               } else {
                   return '-';
               }
               numberSpecialCharacters++; 
            }
        }
        if (isUsingNumericCharacters) {
            if ( random >= 48 && random <= 57 ) {
                returnCharacter = (char) random;
            }
        }
        
        if (isUsingUpperCaseCharacters) {
            if ( random >= 65 && random <= 90 ) {
                returnCharacter = (char) random;
            }
        }
        
        if (isUsingLowerCaseCharacters) {
            if ( random >= 97 && random <= 122 ) {
                returnCharacter = (char) random;
            }
        }

        if ( returnCharacter == 0 ) {
            random = randomGenerator.nextInt( 25 ) + 65;
            if ( isUsingUpperCaseCharacters ) {
                returnCharacter = (char) random;
            } else {
                returnCharacter = (char) (random + 32);
            }
        }
        
        return returnCharacter;
        
    }

    /**
     * @return the isUsingSpecialCharacters
     */
    public boolean isUsingSpecialCharacters() {
        return isUsingSpecialCharacters;
    }

    /**
     * @param isUsingSpecialCharacters the isUsingSpecialCharacters to set
     */
    public void setIsUsingSpecialCharacters(boolean isUsingSpecialCharacters) {
        this.isUsingSpecialCharacters = isUsingSpecialCharacters;
    }

    /**
     * @return the isUsingUpperCaseCharacters
     */
    public boolean isUsingUpperCaseCharacters() {
        return isUsingUpperCaseCharacters;
    }

    /**
     * @param isUsingUpperCaseCharacters the isUsingUpperCaseCharacters to set
     */
    public void setIsUsingUpperCaseCharacters(boolean isUsingUpperCaseCharacters) {
        this.isUsingUpperCaseCharacters = isUsingUpperCaseCharacters;
    }

    /**
     * @return the isUsingLowerCaseCharacters
     */
    public boolean isUsingLowerCaseCharacters() {
        return isUsingLowerCaseCharacters;
    }

    /**
     * @param isUsingLowerCaseCharacters the isUsingLowerCaseCharacters to set
     */
    public void setIsUsingLowerCaseCharacters(boolean isUsingLowerCaseCharacters) {
        this.isUsingLowerCaseCharacters = isUsingLowerCaseCharacters;
    }

    /**
     * @return the isUsingNumericCharacters
     */
    public boolean isUsingNumericCharacters() {
        return isUsingNumericCharacters;
    }

    /**
     * @param isUsingNumericCharacters the isUsingNumericCharacters to set
     */
    public void setIsUsingNumericCharacters(boolean isUsingNumericCharacters) {
        this.isUsingNumericCharacters = isUsingNumericCharacters;
    }
    
}
